package com.mapfinal.server;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.mapfinal.event.Event;
import com.mapfinal.map.MapScene;
import com.mapfinal.platform.bufferedimage.BufferedImagePickRenderEngine;
import com.mapfinal.platform.bufferedimage.BufferedImageRenderEngine;
import com.mapfinal.render.RenderEngine;
import com.mapfinal.render.pick.PickRenderEngine;

public class ServerMap extends MapScene {

	// 渲染引擎
	private BufferedImageRenderEngine engine;
	// 拾取渲染
	private BufferedImagePickRenderEngine pickEngine;
	// 是否拾取
	private boolean pick = false;
	// 通用事件参数
	private Event event;
	// 渲染到的图片
	private BufferedImage image = null;
	
	public ServerMap() {
		super();
		setWidth(256);
		setHeight(256);
		this.image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
		this.engine = new BufferedImageRenderEngine();
		this.pickEngine = new BufferedImagePickRenderEngine(2, 2);
		this.event = new Event("render");
	}
	
	public ServerMap(int width, int height) {
		super();
		setWidth(width);
		setHeight(height);
		this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		this.engine = new BufferedImageRenderEngine();
		this.pickEngine = new BufferedImagePickRenderEngine(2, 2);
		this.event = new Event("render");
	}

	public ServerMap(int width, int height, int imageType) {
		super();
		setWidth(width);
		setHeight(height);
		this.image = new BufferedImage(width, height, imageType);
		this.engine = new BufferedImageRenderEngine();
		this.pickEngine = new BufferedImagePickRenderEngine(2, 2);
		this.event = new Event("render");
	}

	@Override
	public RenderEngine getRenderEngine() {
		return engine;
	}
	
	@Override
	public PickRenderEngine getPickEngine() {
		return pickEngine;
	}
	
	public void putEventData(String name, Object data) {
		event.put(name, data);
	}

	public BufferedImage drawTo(BufferedImage bufferedImage) {
		if(bufferedImage==null) {
			bufferedImage =  new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
		}
		draw(bufferedImage.getGraphics(), 256, 256);
		return bufferedImage;
	}
	
	public ServerImage drawTo(ServerImage tile) {
		draw(tile.getImage().getGraphics(), tile.getWidth(), tile.getHeight());
		return tile;
	}
	
	public BufferedImage draw() {
		draw(image.getGraphics(), getWidth(), getHeight());
		return image;
	}
	
	public void draw(Graphics g, int width, int height) {
		engine.setGraphics(g);
		setWidth(width);
		setHeight(height);
		Event eventRender = Event.by("render", event);
		eventRender.set("width", getWidth()).set("height", getHeight());
		this.draw(eventRender, engine);
	}

	public void drawPick(float x, float y) {
		setWidth(pickEngine.getWidth());
		setHeight(pickEngine.getHeight());
		Event eventRender = Event.by("pick", event);
		eventRender.set("width", getWidth()).set("height", getHeight());
		this.pick(eventRender, pickEngine, x, y);
		pick = true;
	}

	@Override
	protected void update() {
		// TODO Auto-generated method stub
		engine.update();
	}

	public boolean getPick() {
		return pick;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}
}
